package com.android.gridview01;

import java.util.Calendar;

import android.app.Activity;
import android.content.SharedPreferences;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class CalendrierActivity extends Activity {
	
	protected static final int MONTHYEARDATESELECTOR_ID = 0;
	
	protected TextView dateText, monthYearText;
	protected GridView gridview;
	protected ImageView flecheGauche, flecheDroite;
	
	//Prefs Objects
	protected static final String PREFS_CALENDRIER = "MyPerseyCalendarPrefs";//Save my calendar prefs
	protected static final Calendar monthYearNow = Calendar.getInstance();//Get the instance of the calendar now
	protected static String PREFS_MOIS_CHOISI = "PREFS_MOIS_CHOISI";//Save my calendar prefs Month
	protected static String PREFS_ANNEE_CHOISIE = "PREFS_ANNEE_CHOISIE";//Save my calendar prefs Year
	
	protected static String PREFS_DATECHOOSEINCALENDAR_JOUR = "PREFS_DATECHOOSEINCALENDAR_JOUR";
	protected static String PREFS_DATECHOOSEINCALENDAR_MOIS = "PREFS_DATECHOOSEINCALENDAR_MOIS";
	protected static String PREFS_DATECHOOSEINCALENDAR_ANNEE = "PREFS_DATECHOOSEINCALENDAR_ANNEE";
	
	protected SharedPreferences myPerseyCalendarPrefs;
	
	//Set Month and Year Store in Prefs
	protected void setPrefsMonthYearChoose(int M, int Y)
	{
		this.myPerseyCalendarPrefs = getSharedPreferences(PREFS_CALENDRIER, MODE_PRIVATE);
		SharedPreferences.Editor prefsEditor = this.myPerseyCalendarPrefs.edit();
		prefsEditor.putInt(PREFS_MOIS_CHOISI, M);//Set Month Choose
		prefsEditor.putInt(PREFS_ANNEE_CHOISIE, Y);//Set Year Choose
		prefsEditor.commit();
	}
	
	//Set The day choosen from calendar in prefs
	protected void setDateChoosenInCalendar(int J, int M, int Y)
	{
		this.myPerseyCalendarPrefs = getSharedPreferences(PREFS_CALENDRIER, MODE_PRIVATE);
		SharedPreferences.Editor prefsEditor = this.myPerseyCalendarPrefs.edit();
		prefsEditor.putInt(PREFS_DATECHOOSEINCALENDAR_JOUR, J);//Set Day Choose
		prefsEditor.putInt(PREFS_DATECHOOSEINCALENDAR_MOIS, M);//Set Month Choose
		prefsEditor.putInt(PREFS_DATECHOOSEINCALENDAR_ANNEE, Y);//Set Year Choose
		prefsEditor.commit();
	}
	
	//Get Day, Month and Year choosen in calendar
	protected int[] getDayInCalendarAndPrefs()
	{
		this.myPerseyCalendarPrefs = getSharedPreferences(PREFS_CALENDRIER, MODE_PRIVATE);
		int dayChoose = this.myPerseyCalendarPrefs.getInt(PREFS_DATECHOOSEINCALENDAR_JOUR, -1);
		int monthChoose = this.myPerseyCalendarPrefs.getInt(PREFS_DATECHOOSEINCALENDAR_MOIS, -1);
		int yearChoose = this.myPerseyCalendarPrefs.getInt(PREFS_DATECHOOSEINCALENDAR_ANNEE, -1);
		int[] val = { dayChoose, monthChoose, yearChoose };//0-Month, 1-Year
		return val;
	}
	
	//Get Month and Year Store in Prefs
	protected int[] getPrefsMonthYearChoose()
	{
		this.myPerseyCalendarPrefs = getSharedPreferences(PREFS_CALENDRIER, MODE_PRIVATE);
		int prefMonthChoose = this.myPerseyCalendarPrefs.getInt(PREFS_MOIS_CHOISI, -1);
		int prefYearChoose = this.myPerseyCalendarPrefs.getInt(PREFS_ANNEE_CHOISIE, -1);
		int[] val = { prefMonthChoose, prefYearChoose };//0-Month, 1-Year
		return val;
	}
	
	protected void moveCalendrierForward()
	{
		this.myPerseyCalendarPrefs = getSharedPreferences(PREFS_CALENDRIER, MODE_PRIVATE);
		int prefMonthChoose = this.myPerseyCalendarPrefs.getInt(PREFS_MOIS_CHOISI, -1);
		int prefYearChoose = this.myPerseyCalendarPrefs.getInt(PREFS_ANNEE_CHOISIE, -1);
		
		Calendar c = Calendar.getInstance();
		c.set(Calendar.MONTH, prefMonthChoose-1);
		c.set(Calendar.YEAR, prefYearChoose);
		c.add(Calendar.MONTH, 1);
		setPrefsMonthYearChoose((c.get(Calendar.MONTH)+1), c.get(Calendar.YEAR));
	}
	
	protected void moveCalendrierBackward()
	{
		this.myPerseyCalendarPrefs = getSharedPreferences(PREFS_CALENDRIER, MODE_PRIVATE);
		int prefMonthChoose = this.myPerseyCalendarPrefs.getInt(PREFS_MOIS_CHOISI, -1);
		int prefYearChoose = this.myPerseyCalendarPrefs.getInt(PREFS_ANNEE_CHOISIE, -1);
		
		Calendar c = Calendar.getInstance();
		c.set(Calendar.MONTH, prefMonthChoose-1);
		c.set(Calendar.YEAR, prefYearChoose);
		c.add(Calendar.MONTH, -1);
		
		setPrefsMonthYearChoose((c.get(Calendar.MONTH)+1), c.get(Calendar.YEAR));
	}
}
